package knowse.app.daos;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.DatabaseUtils.InsertHelper;
import android.database.sqlite.SQLiteDatabase;

public class TestStatusDetailDAO {
	private final String TAG = "TestStatusDetailDAO";
	private final String ERRORMSG = "Insertion failed";
	protected static final String Table = "TestStatusDetail";
	protected static final String Location = "Location";
	protected static final String Class_ = "Class";
	protected static final String Method = "Method";
	protected static final String Message = "Message";
	protected static final String Type = "Type";
	protected static final String ID = "_ID";
	protected static final String Backtrace="Backtrace";
	//foreign key.
	protected static final String TestStatusTimeStamp = "TestStatusTimeStamp";
	
	public TestStatusDetailDAO(){
		
	}
	
	
	
	//insert methods to retrieve and update the database

	/*
	 * TODO
	 */
	public void store(TestStatusDetailDataObject tdo) throws Exception{
		ContentValues values= new ContentValues();
		values.put(Class_, tdo.getTestClass());
		values.put(Location, tdo.getLocation());
		values.put(Message, tdo.getMessage());
		values.put(Method, tdo.getMethod());
		values.put(Type, tdo.getType());
		values.put(Backtrace, tdo.getBacktrace());
		values.put(TestStatusTimeStamp, tdo.getTimeStamp().getTime());
//		values.putNull(ID);
		
		//TODO
		DBHelper dbHelper=new DBHelper();
		
		SQLiteDatabase db=dbHelper.getWritableDatabase();
		
		long insertResult = db.insert(Table, null, values);
		if(insertResult < 0){
			throw new Exception(TAG + " " + ERRORMSG);
		}
		db.close();
	}
	
	/*
	 * V1. Store using InsertHelper and Transactions
	 * 
	 */
	public void storeIHTrans(ArrayList<TestStatusDetailDataObject> list) throws Exception{
		DBHelper dbHelper = new DBHelper();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		InsertHelper ih = new InsertHelper(db,Table);
		int classCol = ih.getColumnIndex(Class_);
		int lCol = ih.getColumnIndex(Location);
		int mCol = ih.getColumnIndex(Message);
		int mthCol = ih.getColumnIndex(Method);
		int tCol = ih.getColumnIndex(Type);
		int bCol = ih.getColumnIndex(Backtrace);
		int timestampCol = ih.getColumnIndex(TestStatusTimeStamp);
		
		db.beginTransaction();
		for(TestStatusDetailDataObject tdo : list){
			ih.prepareForInsert();
			ih.bind(classCol, tdo.getTestClass());
			ih.bind(lCol, tdo.getLocation());			
			ih.bind(mCol, tdo.getMessage());
			ih.bind(mthCol, tdo.getMethod());
			ih.bind(tCol, tdo.getType());
			ih.bind(bCol, tdo.getBacktrace());
			ih.bind(timestampCol, tdo.getTimeStamp().getTime());
			ih.execute();
		}
		db.setTransactionSuccessful();
		db.endTransaction();
		ih.close();
		db.close();
	}
	
	/*
	 * V2. Store using Transactions
	 */
	public void storeTrans(ArrayList<TestStatusDetailDataObject> list) throws Exception{
		DBHelper dbHelper = new DBHelper();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		for(TestStatusDetailDataObject tdo : list){
			ContentValues values= new ContentValues();
			values.put(Class_, tdo.getTestClass());
			values.put(Location, tdo.getLocation());
			values.put(Message, tdo.getMessage());
			values.put(Method, tdo.getMethod());
			values.put(Type, tdo.getType());
			values.put(Backtrace, tdo.getBacktrace());
			values.put(TestStatusTimeStamp, tdo.getTimeStamp().getTime());
			Long insertResult = db.insert(Table, null, values);
			if(insertResult < 0){
				throw new Exception(TAG + " " + ERRORMSG);
			}
		}
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}
	
	
}
